/* Copyright 2011 Rok Lenarčič 
 * 
 * This file is a part of MicroFn.
 * 
 * MicroFn is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * MicroFn is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/> */

package net.rlenar.microfn.func;

import java.util.Iterator;

import net.rlenar.microfn.EmptyIterable;
import net.rlenar.microfn.Fn;
import net.rlenar.microfn.PrettyPrint;

/**
 * This class wraps an array into an iterable. The iterable is also a Fn of one argument,
 * which is used as an index into the array.
 * 
 * @author Rok Lenarcic
 *
 */
public class ArraySeq<T> extends Fn implements Iterable<T> {
	private final T[] arr;
	public ArraySeq(T[] arr) {
		super(1,1);
		this.arr = arr;
	}
	@Override
	public T doFunc(Object... args) {
		return arr[((Number)args[0]).intValue()];
	}
	@SuppressWarnings("unchecked")
	public Iterator<T> iterator() {
		if (arr == null || arr.length == 0) {
			return (Iterator<T>) EmptyIterable.EMPTY_ITERABLE;
		}
		return new Iterator<T>() {
			int i = 0;
			public boolean hasNext() {
				return i < arr.length;
			}
			public T next() {
				i++;
				return arr[i-1];
			}
			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}
	

	public static <T> ArraySeq<T> $A(T[] array) {
		return new ArraySeq<T>(array);
	}

	public static Fn $A = new Fn(1,1) {
		@Override
		protected Object doFunc(Object... args) {
			return new ArraySeq<Object>((Object[])args[0]);
		}	
		
	};
	
	@Override
	public boolean equals(Object o) {
		if(!(o instanceof Iterable)) {
			return false;
		}
		@SuppressWarnings("unchecked")
		Iterator<? extends Object> otherArr = ((Iterable<? extends Object>)o).iterator();
		if (arr != null) {
			for (int i=0;i < arr.length;i++) {
				if (!otherArr.hasNext()) { // if other iterable has no elements then it's too short
					return false;
				} else if (arr[i] == null && otherArr.next() != null) {
					return false;
				} else if (!arr[i].equals(otherArr.next())) { 
					return false;
				}
			}
		}
		// if other iterable has more elements then they are not same length
		if (otherArr.hasNext()) {
			return false;
		}
		return true;
	}
	private int hashCode = -1;
	@Override
	public int hashCode() {
		if (hashCode == -1) {
			int i = 1;
			if (arr != null) {
				for (T o : arr) {
					i = i * 29 + o.hashCode();
				}
			}
			hashCode = i;
		}
		return hashCode;
	}
	@Override
	public String toString() {
		return PrettyPrint.prettyPrint(this);
	}
}
